var app = new Vue({
	el: '#app',
	data: {
		bankNo: '', //储蓄卡号
		tel: '', //手机号
		checkCode: '', //验证码
		bankNoInputType: "text", //银行卡号输入框初始值
		count: 60,
		timer: () => {},
		showLogo: true, //是否显示底部logo
	},
	computed: {
		disabled() {
			if(this.bankNo && this.tel && this.checkCode) {
				return false;
			} else {
				return true;
			}
		},
		checkCodeBtnDisabled() {
			if(this.bankNoPureNum && this.tel && this.count === 60) {
				return false;
			}
			return true;
		},
		bankNoPureNum() {
			return this.bankNo.replace(/\s*/g, "")
		},
		sendCodeText() {
			if(this.count >= 60) {
				return "发送验证码"
			} else {
				return `再次发送 | ${this.count}s`
			}
		}
	},
	created() {
		const localData = utils.getItem('step2');
		if(localData) {
			for(let [key, value] of Object.entries(localData)) {
				this[key] = value;
			}
		}
	},
	beforeDestory() {
		this.timer && clearInterval(this.timer);
	},
	methods: {
		jump(path) {
			utils.jump(path);
			utils.setItem('step2', {bankNo: this.bankNo, tel: this.tel});
		},
		onBanckCardFocus: function() {
			this.bankNoInputType = "tel";
		},
		onBanckCardBlur: function() {
			this.bankNoInputType = "text";
		},
		onBanckCardKeyup: function(event) {
			var e = event.target.value;
			this.bankNo = e.replace(/\s/g, '').replace(/(\d{4})(?=\d)/g, "$1 ");
		},
		goNextStep: function() {
			//			 校验银行卡，手机号
			if(this.disabled) {
				return;
			}
			const {bankCardTested, telRegTested} = this.checkReg();
			if(!bankCardTested) {
				this.$toast('请输入正确储蓄卡号');
				return;
			}
			if(!telRegTested) {
				this.$toast('请输入正确的手机号');
				return;
			};
			this.jump('register-step3');
		},
		checkReg: function() {
			const bankCardReg = new RegExp(regExpConfig.bankCard);
			const telReg = new RegExp(regExpConfig.mobile);
			let bankCardTested = true; let telRegTested = true;
			if(!bankCardReg.test(this.bankNoPureNum)) {
				bankCardTested = false;
			}
			if(!telReg.test(this.tel)) {
				telRegTested = false;
			};
			return {bankCardTested, telRegTested}
		},
		sendCheckCode: function() {
			if(this.checkCodeBtnDisabled) {
				return;
			}
			this.$refs.checkCodeInput.focus();
			const {bankCardTested, telRegTested} = this.checkReg();
			if(!bankCardTested) {
				this.$toast('请输入正确储蓄卡号');
				return;
			}
			if(!telRegTested) {
				this.$toast('请输入正确的手机号');
				return;
			};
			this.count--;
			this.timer = setInterval(() => {
				if(this.count > 1) {
					this.count -- ;
				} else {
					clearInterval(this.timer);
					this.count = 60;
				}
			}, 1000);
		}
	}
});